function formatTime(time) {
	if (typeof time !== 'number' || time < 0) {
		return time
	}

	var hour = parseInt(time / 3600)
	time = time % 3600
	var minute = parseInt(time / 60)
	time = time % 60
	var second = time

	return ([hour, minute, second]).map(function (n) {
		n = n.toString()
		return n[1] ? n : '0' + n
	}).join(':')
}

function formatLocation(longitude, latitude) {
	if (typeof longitude === 'string' && typeof latitude === 'string') {
		longitude = parseFloat(longitude)
		latitude = parseFloat(latitude)
	}

	longitude = longitude.toFixed(2)
	latitude = latitude.toFixed(2)

	return {
		longitude: longitude.toString().split('.'),
		latitude: latitude.toString().split('.')
	}
}
var dateUtils = {
	UNITS: {
		'年': 31557600000,
		'月': 2629800000,
		'天': 86400000,
		'小时': 3600000,
		'分钟': 60000,
		'秒': 1000
	},
	humanize: function (milliseconds) {
		var humanize = '';
		for (var key in this.UNITS) {
			if (milliseconds >= this.UNITS[key]) {
				humanize = Math.floor(milliseconds / this.UNITS[key]) + key + '前';
				break;
			}
		}
		return humanize || '刚刚';
	},
	format: function (dateStr) {
		var date = this.parse(dateStr)
		var diff = Date.now() - date.getTime();
		if (diff < this.UNITS['天']) {
			return this.humanize(diff);
		}
		var _format = function (number) {
			return (number < 10 ? ('0' + number) : number);
		};
		return date.getFullYear() + '/' + _format(date.getMonth() + 1) + '/' + _format(date.getDate()) + '-' +
			_format(date.getHours()) + ':' + _format(date.getMinutes());
	},
	parse: function (str) { //将"yyyy-mm-dd HH:MM:ss"格式的字符串，转化为一个Date对象
		var a = str.split(/[^0-9]/);
		return new Date(a[0], a[1] - 1, a[2], a[3], a[4], a[5]);
	},
	getNowTime: function () {
		let dateTime
		let yy = new Date().getFullYear()
		let mm = (new Date().getMonth() + 1) < 10? '0' + (new Date().getMonth() + 1) : new Date().getMonth() + 1
		let dd = new Date().getDate() < 10 ? '0' + new Date().getDate() : new Date().getDate()
		let hh = new Date().getHours() < 10 ? '0' + new Date().getHours() : new Date().getHours()
		let mf = new Date().getMinutes() < 10 ? '0' + new Date().getMinutes()
		:
		new Date().getMinutes()
		let ss = new Date().getSeconds() < 10 ? '0' + new Date().getSeconds()
		:
		new Date().getSeconds()
		dateTime = yy + '-' + mm + '-' + dd + ' ' + hh + ':' + mf + ':' + ss
		return dateTime
	},
	getNowDate: function () {
		let date
		let yy = new Date().getFullYear()
		let mm = (new Date().getMonth() + 1) < 10? '0' + (new Date().getMonth() + 1) : new Date().getMonth() + 1
		let dd = new Date().getDate() < 10 ? '0' + new Date().getDate() : new Date().getDate()
		date = yy + '-' + mm + '-' + dd
		return date
	},
	formatByDate: function (dateStamp) {
		let dateObj = new Date(dateStamp)
		let date
		let yy = dateObj.getFullYear()
		let mm = (dateObj.getMonth() + 1) < 10? '0' + (dateObj.getMonth() + 1) : dateObj.getMonth() + 1
		let dd = dateObj.getDate() < 10 ? '0' + dateObj.getDate() : dateObj.getDate()
		date = yy + '-' + mm + '-' + dd
		return date
	},
	formatByStamp: function(timeStamp) {
		let dateObj = new Date(timeStamp)
		let dateTime
		let yy = dateObj.getFullYear()
		let mm = (dateObj.getMonth() + 1) < 10 ? '0' + (dateObj.getMonth() + 1) : dateObj.getMonth() + 1
		let dd = dateObj.getDate() < 10 ? '0' +dateObj.getDate() : dateObj.getDate()
		let hh = dateObj.getHours() < 10 ? '0' + dateObj.getHours() : dateObj.getHours()
		let mf = dateObj.getMinutes() < 10 ? '0' + dateObj.getMinutes()
		:
		dateObj.getMinutes()
		let ss = dateObj.getSeconds() < 10 ? '0' + dateObj.getSeconds()
		:
		dateObj.getSeconds()
		dateTime = yy + '-' + mm + '-' + dd + ' ' + hh + ':' + mf + ':' + ss
		return dateTime
	},
	formatByStamp2: function(timeStamp) {
		let dateObj = new Date(timeStamp)
		let dateTime
		let yy = dateObj.getFullYear()
		let mm = (dateObj.getMonth() + 1) < 10 ? '0' + (dateObj.getMonth() + 1) : dateObj.getMonth() + 1
		let dd = dateObj.getDate() < 10 ? '0' +dateObj.getDate() : dateObj.getDate()
		let hh = dateObj.getHours() < 10 ? '0' + dateObj.getHours() : dateObj.getHours()
		let mf = dateObj.getMinutes() < 10 ? '0' + dateObj.getMinutes()
		:
		dateObj.getMinutes()
		let ss = dateObj.getSeconds() < 10 ? '0' + dateObj.getSeconds()
		:
		dateObj.getSeconds()
		if (new Date(Number(timeStamp)).toDateString() === new Date().toDateString()) {
		  dateTime = '今天'+ hh + ':' + mf
		}else{
			dateTime = mm + '-' + dd + ' ' + hh + ':' + mf
		}
		return dateTime
	},
	formatChTime:function(dateTime){
		return (new Date(dateTime)).getTime()
	},
	//时间差计算
	formatChatTime:function(sj) {
		var nowt = new Date().getTime();
		var a = new Date(parseInt(sj));
		var b = new Date(parseInt(nowt));
		var date1 = Date.parse(this.formatChatType(a, 4));
		var date2 = Date.parse(this.formatChatType(b, 4));
		var xxsj = Math.ceil((date2 - date1) / (60 * 1000))
		if (xxsj <= 1 && xxsj >= 0) {
			return "就刚才";
		} else if (xxsj <= 10 && xxsj > 1) {
			return xxsj + "分钟前";
		} else if (xxsj <= 60 && xxsj > 10) {
			return this.formatChatType(a, 1);
		} else if (xxsj <= 1440 && xxsj > 60) {
			return this.formatChatType(a, 1);
		} else if (xxsj <= 10080 && xxsj > 1440) {
			return this.formatChatType(a, 2);
		} else if (xxsj > 10080) {
			return this.formatChatType(a, 3);
		} else {
			return this.formatChatType(a, 3);
		}
	},
	//格式化时间
	formatChatType:function(now, type) {
		var show_day = new Array('星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六');
		var year = now.getFullYear().toString();
		var month = (now.getMonth() + 1).toString();
		var day = (now.getDate()).toString();
		var tian = now.getDay().toString();
		var hour = (now.getHours()).toString();
		var minute = (now.getMinutes()).toString();
		var second = (now.getSeconds()).toString();
		if (hour.length == 1) {
			hour = "0" + hour;
		}
		if (minute.length == 1) {
			minute = "0" + minute;
		}
		if (second.length == 1) {
			second = "0" + second;
		}
		if (type == 1) {
			var dateTime = hour + ":" + minute;
		} else if (type == 2) {
			var dateTime = show_day[tian] + " " + hour + ":" + minute
		} else if (type == 3) {
			var dateTime = year + "-" + month + "-" + day
		} else if (type == 4) {
			var dateTime = year + "/" + month + "/" + day + " " + hour + ":" + minute + ":" + second;
		} else if (type == 5) {
			var dateTime = show_day[tian];
		}
		return dateTime;
	}
};

// 处理脱敏
function dataRemoval(str, begin, end, maskStr) {
	// 参数说明：str： 需要脱敏处理的数据，
	// 		 begin: 前置明文字符数
	// 		 end： 后置明文字符数
	// 		 maskStr: 替换的字符，一般为*
 	if (!str) return ''
 	if (!str && (brgin + end) >= str.length) {
 		return ''
 	}
 	let leftStr = str.substring(0,begin)
 	let rightStr = str.substring(str.length-end, str.length)
 	let strCon = ''
	for (let i = 0; i < str.length - begin - end; i++) {
		strCon += maskStr
	}
	return leftStr + strCon + rightStr;
};
function  copyData(data){
	// 复制
	uni.setClipboardData({
		data: data,// 复制的内容 
		success: function () {
			uni.showToast({
				title:'复制成功',
				duration:2000
			})
		},
		fail() {
			uni.showToast({
				title:'复制失败',
				icon:'none',
				duration:2000
			})
		}
	});
};

module.exports = {
	formatTime: formatTime,
	formatLocation: formatLocation,
	dateUtils: dateUtils,
	dataRemoval: dataRemoval,
	copyData:copyData
}
